// 变量声明
extern float Temperature;
extern float Humidity;
extern float Turbidity;
extern float PH;
extern float Hardness;
extern float Sulfate;
// 函数声明
void btnLed_callback(const String &state);
void heartbeat();
void blinkerSetUp();

// 新建组件对象
// 数据

char num_tem[] = "num-tem";
char num_hum[] = "num-hum";
char num_tu[] = "num-tu";
char num_ph[] = "num-ph";
char num_hard[] = "num-hard";
char num_sulfate[] = "num-sulfate";
char btn_led[] = "btn-led";

BlinkerNumber numTem(num_tem);
BlinkerNumber numHum(num_hum);
BlinkerNumber numTu(num_tu);
BlinkerNumber numPh(num_ph);
BlinkerNumber numHard(num_hard);
BlinkerNumber numSulfate(num_sulfate);
// led开关
BlinkerButton btnLed(btn_led);

// 按下按键 btnLedOn 即会执行该函数
void btnLed_callback(const String &state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == "on")
    {
        digitalWrite(ledPin, HIGH);
        // 反馈开关状态
        btnLed.print("on");
    }
    else if (state == "off")
    {
        digitalWrite(ledPin, LOW);
        // 反馈开关状态
        btnLed.print("off");
    }
}

// 心跳包函数
void heartbeat()
{
    numTem.print(Temperature);
    numHum.print(Humidity);
    numTu.print(Turbidity);
    numPh.print(PH);
    numHard.print(Hardness);
    numSulfate.print(Sulfate);
}

void blinkerSetUp()
{
    // BLINKER_DEBUG.stream(Serial);
    // BLINKER_DEBUG.debugAll();
    // 初始化esp自带的led引脚
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
    // 绑定回调函数
    btnLed.attach(btnLed_callback);
    // 心跳包函数
    Blinker.attachHeartbeat(heartbeat);
    // 初始化blinker
    Blinker.begin(blinkerDeviceKey, wifiName, wifiPassword);
}